package com.yuan.socket.data;

import cn.constant.CoinConstant;
import cn.model.Pairs;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuan.common.redis.service.RedisRepository;
import com.yuan.socket.mapper.PairsMapper;
import com.yuan.socket.server.Websocket;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@EnableScheduling
@Order(value = 1)
@Component
@Slf4j
public class DataService implements ApplicationRunner {
    @Autowired
    public PairsMapper pairsMapper;
    @Autowired
    public RedisRepository redisRepository;
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("启动websocket");
        KlineInitLoad();
        Websocket.start(this);
    }
    //@Scheduled(fixedRate = 2000)
    public void KlineInitLoad() throws InterruptedException {
        QueryWrapper<Pairs> queryWrapper =  new QueryWrapper<>();
        queryWrapper.eq("state", CoinConstant.Coin_State.NORMAL);
        List<Pairs> pairslist =  pairsMapper.selectList(queryWrapper);
        ThreadPoolExecutor es = new ThreadPoolExecutor(50, 50, 0L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(), r -> {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        });
        for (Pairs pair:pairslist){
            es.submit(new LoadInitKline(pair.getPairsName(),redisRepository));
        }
        es.shutdown();
//        Thread.sleep(2000);
//        String targetThreadName = "Thread-16";
//        if (isThreadAliveByName(targetThreadName)) {
//            System.out.println(" 线程 " + targetThreadName + " 存在");
//        } else {
//            System.out.println(" 线程 " + targetThreadName + " 不存在");
//        }
//        for (int i = 0; i < 5; i++) {
//            es.execute(() -> {
//                try {
//                    Thread.sleep(10000);
//                    System.out.println("sendAllMessageKline:"+System.currentTimeMillis());
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            });
//        }
//        log.info("getQueue {}","getQueue:"+es.getQueue().size());
//        log.info("KlineInitLoad {}","KlineInitLoad:"+System.currentTimeMillis());
    }

    public static class LoadInitKline implements Runnable {
        private RedisRepository redisRepositorys;
        private String pairsNames;
        public LoadInitKline(String pairsName, RedisRepository redisRepository) {
           this.pairsNames = pairsName;
           this.redisRepositorys = redisRepository;
        }
        @Override
        public void run() {
            //redisRepositorys.zsetAdd("test_set_add",pairsNames,System.currentTimeMillis());
            Set<String> set = redisRepositorys.zsetRevRang("test_set_add",0,-2);
            System.out.println("集合内容"+set);
            //System.out.println("执行任务："+pairsNames+"线程："+ Thread.currentThread().getName());
        }
    }
//    public static boolean isThreadAliveByName(String threadName) {
//        Set<Thread> threads = Thread.getAllStackTraces().keySet();
//        for (Thread t : threads) {
//            if (t.getName().equals(threadName))  {
//                return true;  // 线程存在
//            }
//        }
//        return false;  // 线程不存在
//    }
}
